<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>布局TabBar | 微博项目的简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../7/README.html" />
    
    
    <link rel="prev" href="../5storyboard_reference/README.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="6" data-basepath=".." data-revision="1457654214927">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         微博项目的简介
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="01/README.html">
            
                
                    <a href="../01/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         项目部署
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="02/README.html">
            
                
                    <a href="../02/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         初始化项目
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="03/README.html">
            
                
                    <a href="../03/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         纯代码搭建项目框架
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="4json/README.html">
            
                
                    <a href="../4json/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         动态创建控制器(了解)
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="5storyboard_reference/README.html">
            
                
                    <a href="../5storyboard_reference/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         StoryBoard Reference
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="6" data-path="6tabbar/README.html">
            
                
                    <a href="../6tabbar/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         布局TabBar
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="7/README.html">
            
                
                    <a href="../7/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         访客视图
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >微博项目的简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_16">
                    
                        <h1 id="tabbar">布局TabBar</h1>
<h3 id="tabbaritems">布局TabBar中的items</h3>
<ul>
<li>Tabbar中应该有5个Item<ul>
<li>主页/消息/发布按钮/发现/我</li>
</ul>
</li>
<li>布局TabBar中的Items可以通过下面的方式<ul>
<li>通过自定义TabBar的方式</li>
<li>多添加一个控制器,让中间空出位置</li>
</ul>
</li>
<li>注意:如果只是在storyboard中设置item的图片,不能设置选中的图片,因此需要通过代码重新设置</li>
</ul>
<pre><code class="lang-swift">    <span class="hljs-comment">/// 图片数组</span>
    private lazy <span class="hljs-keyword">var</span> imageNames : [<span class="hljs-type">String</span>] = {
        <span class="hljs-keyword">return</span> [<span class="hljs-string">"tabbar_home"</span>, <span class="hljs-string">"tabbar_message_center"</span>, <span class="hljs-string">""</span> ,<span class="hljs-string">"tabbar_discover"</span>, <span class="hljs-string">"tabbar_profile"</span>]
    }()

    private <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">adjustItems</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..&lt;tabBar.items!.<span class="hljs-built_in">count</span> {
            <span class="hljs-comment">// 1.取出item</span>
            <span class="hljs-keyword">let</span> item = tabBar.items![i]

            <span class="hljs-comment">// 2.如果是第二个item,则不可以和用户交互.并且不需要设置图片</span>
            <span class="hljs-keyword">if</span> i == <span class="hljs-number">2</span> {
                item.enabled = <span class="hljs-built_in">false</span>
                <span class="hljs-keyword">continue</span>
            }

            <span class="hljs-comment">// 2.设置图片</span>
            item.image = <span class="hljs-type">UIImage</span>(named: imageNames[i])
            item.selectedImage = <span class="hljs-type">UIImage</span>(named: imageNames[i] + <span class="hljs-string">"_highlighted"</span>)
        }
    }
</code></pre>
<h3 id="">添加&#39;+&#39;按钮</h3>
<ul>
<li>创建UIButton<ul>
<li>扩展UIButton的构造函数</li>
</ul>
</li>
</ul>
<pre><code class="lang-swift">    convenience <span class="hljs-keyword">init</span>(imageName : <span class="hljs-type">String</span>, bgImageName : <span class="hljs-type">String</span>) {
        <span class="hljs-keyword">self</span>.<span class="hljs-keyword">init</span>()

        <span class="hljs-comment">// 设置相关属性</span>
        setBackgroundImage(<span class="hljs-type">UIImage</span>(named: bgImageName), forState: .<span class="hljs-type">Normal</span>)
        setBackgroundImage(<span class="hljs-type">UIImage</span>(named: bgImageName + <span class="hljs-string">"_highlighted"</span>), forState: .<span class="hljs-type">Highlighted</span>)
        setImage(<span class="hljs-type">UIImage</span>(named: imageName), forState: .<span class="hljs-type">Normal</span>)
        setImage(<span class="hljs-type">UIImage</span>(named: imageName + <span class="hljs-string">"_highlighted"</span>), forState: .<span class="hljs-type">Highlighted</span>)
        sizeToFit()
    }
</code></pre>
<ul>
<li>通过懒加载创建UIButton,并且在viewWillAppear中添加按钮</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-comment">/// 发布微博按钮</span>
    private lazy <span class="hljs-keyword">var</span> composeBtn : <span class="hljs-type">UIButton</span> = {
        <span class="hljs-comment">// let btn = UIButton.createButton("tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")</span>
        <span class="hljs-keyword">let</span> btn = <span class="hljs-type">UIButton</span>(imageName: <span class="hljs-string">"tabbar_compose_icon_add"</span>, bgImageName: <span class="hljs-string">"tabbar_compose_button"</span>)
        btn.center = <span class="hljs-type">CGPoint</span>(x: <span class="hljs-keyword">self</span>.tabBar.bounds.width * <span class="hljs-number">0.5</span>, y: <span class="hljs-keyword">self</span>.tabBar.bounds.height * <span class="hljs-number">0.5</span>)

        <span class="hljs-comment">// 监听按钮的点击</span>
        <span class="hljs-comment">// Selector("composeBtnClick")</span>
        <span class="hljs-comment">// "composeBtnClick"</span>
        btn.addTarget(<span class="hljs-keyword">self</span>, action: <span class="hljs-type">Selector</span>(<span class="hljs-string">"composeBtnClick"</span>), forControlEvents: .<span class="hljs-type">TouchUpInside</span>)

        <span class="hljs-keyword">return</span> btn
    }()

    <span class="hljs-keyword">override</span> <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">viewWillAppear</span><span class="hljs-params">(animated: Bool)</span> </span>{
        <span class="hljs-keyword">super</span>.viewWillAppear(animated)

        <span class="hljs-comment">// 1.调整items</span>
        adjustItems()

        <span class="hljs-comment">// 2.添加`加号`按钮</span>
        tabBar.addSubview(composeBtn)
    }
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../5storyboard_reference/README.html" class="navigation navigation-prev " aria-label="Previous page: StoryBoard Reference"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../7/README.html" class="navigation navigation-next " aria-label="Next page: 访客视图"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
